package com.zzk.lanouhn.myapplication1.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.InvestPingLunAuthorGuanzhuAdapter;
import com.zzk.lanouhn.myapplication1.modles.InvestPingLunAuthorGuanzhuModle;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 关注
 * Created by lanouhn on 16/9/19.
 */
public class InvestPinglunAuthorGuanzhu extends AppCompatActivity {

    private XRecyclerView xRecyclerView;
    //返回
    private ImageView iv_fanhui;
    private String guanzhuID;

    private InvestPingLunAuthorGuanzhuAdapter guanzhuAdapter;
    private List<InvestPingLunAuthorGuanzhuModle.DataBean.AttentionDataBean> mlist = new ArrayList<>();
    private List<InvestPingLunAuthorGuanzhuModle.DataBean.AttentionDataBean> templist = new ArrayList<>();
    private int page = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invest_pinglun_author_guanzhu);
        xRecyclerView = (XRecyclerView) findViewById(R.id.pinglun_author_guanzhu_xrecyclerView);
        iv_fanhui = (ImageView) findViewById(R.id.pinglun_author_guanzhu_fanhui);

        LinearLayoutManager layoutManager = new LinearLayoutManager(InvestPinglunAuthorGuanzhu.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(layoutManager);

        //上拉加载的动画效果
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //recyclerView.setLaodingMoreProgressStyle(ProgressStyle.Pacman);
        //下拉刷新的动画效果
        xRecyclerView.setArrowImageView(R.drawable.ic_loading_rotate);

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                page = 1;
                getGuanzhuData();
            }

            //上拉加载
            @Override
            public void onLoadMore() {
                page++;
                getGuanzhuData();
            }
        });
        guanzhuID = getIntent().getStringExtra("guanzhuID");

        getGuanzhuData();
        initData();
    }

    private void getGuanzhuData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://user.zhangyoubao.com/apis/rest/UsersService/getUserAttentions?" +
                        "page=" + page + "&userid=" + guanzhuID + "&i_=K5h39AQJHAgxlB3k7XOKge9pJXaie1ZTEv3udZmXycc=" +
                        "&deviceId=K5h39AQJHAgxlB3k7XOKge9pJXaie1ZTEv3udZmXycc=&t_=1474191797786&p_=" +
                        "25703&v_=400902&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=22&cha=baiduMartket&u_=&modle";

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String ss = response.body().string();
                        Gson gson = new Gson();

                        templist = gson.fromJson(ss, InvestPingLunAuthorGuanzhuModle.class).getData().getAttentionData();

                        if (templist.size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }
                    }
                });
            }
        }).start();
    }

    //返回
    private void initData() {
        iv_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(InvestPinglunAuthorGuanzhu.this, "失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    if (page == 1) {
                        mlist.clear();
                        mlist.addAll(templist);
                        guanzhuAdapter = new InvestPingLunAuthorGuanzhuAdapter(mlist, InvestPinglunAuthorGuanzhu.this);
                        xRecyclerView.setAdapter(guanzhuAdapter);

                        xRecyclerView.refreshComplete();
                        templist.clear();
                    } else {
                        mlist.addAll(templist);
                        guanzhuAdapter.notifyDataSetChanged();
                        xRecyclerView.loadMoreComplete();
                        templist.clear();
                    }
                    break;
                case 2:

                    break;
            }
        }
    };
}
